﻿using System;
using NLog;
using ScrappyDB;
using NUnit.Framework;
using ScrappyDB.Attributes;
using ScrappyDB.BaseClasses;
using ScrappyDB.Utilities;

namespace ScrappyDB.IntegrationTests
{


    /// <summary>
    ///This is a test class for LatLonProximityTest and is intended
    ///to contain all LatLonProximityTest Unit Tests
    ///</summary>
    [TestFixture]
    public class LatLonProximityTest
    {
        private static readonly Logger log = LogManager.GetCurrentClassLogger();

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [TestFixtureSetUp]
        public  void Init()
        {
            MyClassCleanup();

            var a = new TestLocation() { Id = Guid.NewGuid(), Latitude = 45, Longitude = -90 }; //center
            var b = new TestLocation() { Id = Guid.NewGuid(), Latitude = 46, Longitude = -90 }; //69 miles away
            var c = new TestLocation() { Id = Guid.NewGuid(), Latitude = 46, Longitude = -91 }; //97 miles away

            var col = new SdbCollection<TestLocation>();
            col.Add(a);
            col.Add(b);
            col.Add(c);

            col.SaveChanges();
        }

        //Use ClassCleanup to run code after all tests in a class have run
        [TestFixtureTearDown]
        public static void MyClassCleanup()
        {
            try
            {
                var db = new Db();
                var col = db.Query<TestLocation>(null,true);

                foreach (var l in col)
                {
                    db.Delete(l);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
        }

        //Use TestInitialize to run code before running each test
        //[SetUp]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TearDown]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [DomainName("ScrappyDB_UnitTests"), EntityName("TestLocation")]
        private class TestLocation : SdbEntityWithLocation
        {
            public Guid Id { get; set; }
        }

        /// <summary>
        ///A test for RemoveCorners
        ///</summary>
        [Test]
        public void RemoveCornersTest()
        {
            double lat = 45F;
            double lon = -90F;
            double searchRadius = 75F;

            var db = new Db();
            var col = db.Query<TestLocation>(null, true);


            Assert.AreEqual(3, col.Count, "This failure means the test class did not successfully initialize the test data");

            LatLonProximity.RemoveCorners(lat, lon, searchRadius, col);

            Assert.AreEqual(2, col.Count); //one record should have been removed
        }



        /// <summary>
        ///A test for GetBoxResults
        ///</summary>
        [Test]
        public void GetBoxResultsTest()
        {
            double lat = 45F;
            double lon = -90F;
            double searchRadius = 75F;

            var db = new Db();
            var col = db.Query<TestLocation>(null, true);

            Assert.AreEqual(3, col.Count);

            var col2 = new SdbCollection<TestLocation>();
            LatLonProximity.GetBoxResults(lat, lon, searchRadius, false, col2,null);

            Assert.AreEqual(3, col2.Count); //no record were removed

        }




        /// <summary>
        ///A test for FindEnititiesByProximity
        ///</summary>
        [Test]
        public void FindEnititiesByProximity()
        {
            double lat = 45F;
            double lon = -90F;
            double searchRadius = 75F;

            var db = new Db();
            var col = db.Query<TestLocation>(null, true);

            Assert.AreEqual(3, col.Count);

            var col2 = LatLonProximity.FindEnititiesByProximity<TestLocation>(lat, lon, searchRadius, false, null);

            Assert.AreEqual(2, col2.Count);
        }
    }
}
